package com.wx.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.wx.dao.CartDao;
import com.wx.model.Books;
import com.wx.model.Cart;
import com.wx.util.DBUtil;

public class CartDaoImpl implements CartDao {

	@Override
	public void addBookToCart(Books book) throws Exception {
		Connection connection = DBUtil.getConnection();
		connection.prepareStatement("insert into cart(bookid,userid,cartcount,price) ");
	}

	@Override
	public List<Cart> checkCart(String username) throws Exception {
		Connection connection = DBUtil.getConnection();
		PreparedStatement ps = connection.prepareStatement("select * from cart where userid=(select userid from users where username=?)");
		ps.setString(1, username);
		ResultSet rs= ps.executeQuery();
		List<Cart> list=new ArrayList<>();
		while(rs.next()){
			Cart cart=new Cart();
			cart.setCartid(rs.getInt(1));
			cart.setBookid(rs.getInt(2));
			cart.setStaid(rs.getInt(3));
			cart.setUserid(rs.getInt(4));
			cart.setCartcount(rs.getInt(5));
			list.add(cart);
		}
		System.out.println(list);
		rs.close();
		ps.close();
		connection.close();
		return list;
	}

	@Override
	public boolean cartDelete(String username,String bookname) throws Exception {
		boolean b=false;
		Connection connection = DBUtil.getConnection();
		PreparedStatement ps = connection.prepareStatement("delete from cart where bookid=(select bookid from books where bookname=?) and userid=(select userid from users where username=?)");
		ps.setString(1, bookname);
		ps.setString(2, username);
		int i = ps.executeUpdate();
		if(i>0){
			b=true;
		}
		ps.close();
		connection.close();
		return b;
	}

}
